package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：Shijiu
 * @create  2019/09/09 15:54
 * @description ：菜单管理的controller
 * @version: 1.0
 */


@RestController
@RequestMapping("/menu")
public class menusController {

    @Reference
    private MenuService menuService;

    /**
     * 查询父级菜单成功
     * @return
     */
    @RequestMapping("/findFirstMenus")
    public Result findFirstMenus(){
        List<Menu> menuList=menuService.findFirstMenus();
        return new Result(true,"查询父级菜单成功",menuList);
    }

    /**
     * 分页查询数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findByPage(queryPageBean);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, "添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加菜单失败");
        }
    }

    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id){
        Menu menu=menuService.findMenuById(id);
        return new Result(true,"查询成功",menu);
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,"编辑菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单失败");
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public  Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,"删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    /*
    * 角色列表要用到的查询父菜单
    * */
    @RequestMapping("/findAllParentMenu")
    public Result findAllParentMenu(){
        try {
            List<Menu> parentMenuList = menuService.findAllParentMenu();
            return new Result(true, MessageConst.GET_MENU_SUCCESS,parentMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }
    }
    /*
     * 角色列表要用到的查询子菜单
     * */
    @RequestMapping("/findAllSubMenu")
    public Result findAllSubMenu(){
        try {
            List<Menu> parentMenuList = menuService.findAllSubMenu();
            return new Result(true, MessageConst.GET_MENU_SUCCESS,parentMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MENU_FAIL);
        }
    }
}
